package edu.maplef.bookwild.dao.impl.jdo;

import com.google.gdata.data.Link;
import com.google.gdata.data.Person;
import com.google.gdata.data.douban.Attribute;
import com.google.gdata.data.douban.SubjectEntry;

import edu.maplef.bookwild.config.Config;
import edu.maplef.bookwild.config.Constants;
import edu.maplef.bookwild.dao.IBookDao;
import edu.maplef.bookwild.schema.Book;

public class BookDaoJDOImpl extends BaseDaoJDOImpl<Book> implements IBookDao {

	private String db_book_url_perfix = Config
			.getConfig(Constants.DB_BOOK_URL_PREFIX);

	public Book entry2Book(SubjectEntry entity) {
		if (entity.getId() == null)
			return null;
		Book b = new Book();
		b.setId(entity.getId());

		//作者
		for (Person p : entity.getAuthors()) {
			b.addAuthor(p.getName());
		}

		//书名
		if (entity.getTitle() != null)
			b.setTitle(entity.getTitle().getPlainText());

		//其他属性
		for (Attribute attr : entity.getAttributes()) {
			//获得isbn10
			if (attr.getName().equalsIgnoreCase("isbn10")) {
				b.setIsbn10(attr.getContent());
				continue;
			}
			//获得isbn13
			if (attr.getName().equalsIgnoreCase("isbn13")) {
				b.setIsbn13(attr.getContent());
				continue;
			}
			//获得页数
			if (attr.getName().equalsIgnoreCase("pages")) {
				b.setPages(attr.getContent());
				continue;
			}
			//获得价格
			if (attr.getName().equalsIgnoreCase("price")) {
				b.setPrice(attr.getContent());
				continue;
			}
			//获得出版商
			if (attr.getName().equalsIgnoreCase("publisher")) {
				b.setPublisher(attr.getContent());
				continue;
			}
			//获得出版日期
			if (attr.getName().equalsIgnoreCase("pubdate")) {
				b.setPubdate(attr.getContent());
				continue;
			}
		}
		//链接地址
		for (Link l : entity.getLinks()) {
			//书籍地址
			if (l.getRel().equalsIgnoreCase("self")) {
				b.setInfolink(l.getHref());
				continue;
			}
			//图片地址
			if (l.getRel().equalsIgnoreCase("image")) {
				b.setImagelink(l.getHref());
			}
		}
		return b;
	}

	@Override
	public Book getLocalBookByNumId(long id) throws Exception {
		if (!db_book_url_perfix.endsWith("/"))
			db_book_url_perfix = db_book_url_perfix + "/";
		String strid = db_book_url_perfix + id;
		return getById(strid);
	}

}